Repository navigation
Reduce cognitive complexity in 5 core functions via helper extraction - #99
Open
sonarqube-agent-dev7[bot] wants to merge 1 commit into
Open
sonarqube-agent-dev7[bot] wants to merge 1 commit into
sonarqube-agent-dev7[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZ-oWkG81o4aY-kM01X9 for python:S3776 rule - AZ-oWkGE1o4aY-kM01Xl for python:S3776 rule - AZ-oWkGN1o4aY-kM01Xt for python:S3776 rule - AZ-oWkFT1o4aY-kM01XC for python:S3776 rule - AZ-oWkF81o4aY-kM01Xd for python:S3776 rule Generated by SonarQube Agent (task: 6571a72c-f641-4f3b-9d64-452eeac847cd)
Author
|
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This PR fixes 5 CRITICAL SonarQube cognitive complexity violations across inference.py, main.py, post_process.py, search_backend.py, and search_manage.py by extracting nested control flow logic into dedicated helper functions. These refactorings improve code maintainability and readability while reducing complexity scores to meet the allowed threshold of 15.
View Project in SonarCloud
Fixed Issues
python:S3776 - Refactor this function to reduce its Cognitive Complexity from 34 to the 15 allowed. • CRITICAL • View issue
Location:
app/inference.py:144Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
Starts reducing the cognitive complexity of the overly complex select_patch function by replacing its header and setup with a new helper, _find_reviewer_approved_patch. The helper takes over the reviewer-approval loop (the for loop and the nested review/json checks), so that branching no longer counts toward select_patch's score. The setup code removed here is re-added further down, in the rewritten select_patch.
python:S3776 - Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed. • CRITICAL • View issue
Location:
app/main.py:559Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
Adds a helper function
_remove_task_output_files. It holds the cleanup logic that used to sit deep insidedo_inference: the loop that deletes every file except the log file and the RawTask metadata files, and removes every subdirectory. The for/if/and/if structure now sits at shallow nesting inside its own function, so its nesting penalties no longer count towarddo_inference's cognitive complexity. This supports the fix for the warning thatdo_inferenceis above the allowed complexity.python:S3776 - Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed. • CRITICAL • View issue
Location:
app/post_process.py:219Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
Introduces a new helper function
_apply_editsthat contains the loop for locating each edit's target file and applying the edit, collecting the indexes of edits that could not be matched or applied. On its own it changes nothing; it exists so the next hunk can move this logic out ofconvert_response_to_diff. That function was flagged for cognitive complexity of 17, above the allowed 15.python:S3776 - Refactor this function to reduce its Cognitive Complexity from 37 to the 15 allowed. • CRITICAL • View issue
Location:
app/search/search_backend.py:759Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
Replaces the nested if/else that splits 'Class.method' names inside get_bug_loc_snippets_new with a call to the new _split_class_method_name helper. This removes the compound condition and nested branch from the overly complex function, lowering its cognitive complexity.
python:S3776 - Refactor this function to reduce its Cognitive Complexity from 34 to the 15 allowed. • CRITICAL • View issue
Location:
app/search/search_manage.py:29Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
Replaces the inline formatting/printing of agent-selected API calls and bug locations (nested ifs and for-loops) in search_iterative with a single call to the new _print_selected_apis helper, removing several nested control-flow increments from the overly complex function.